package com.uphone.kingmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.china.zhuoyue.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : 王雨
 *     e-mail :
 *     time   : 2018/07/31
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class RuZhuAdapter extends RecyclerView.Adapter<RuZhuAdapter.MyHolder>{

    private Context mContext;
    private final LayoutInflater inflater;
    private List<File> list=new ArrayList<>();
    private final int MAX_PCITURE = 2;
    private itemOnClickListener listener = null;
    private itemDeleteOnClickListener deletelistener = null;

    public RuZhuAdapter(Context context) {
        mContext=context;
        inflater = LayoutInflater.from(mContext);
    }


    public void setList(List<File> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view=inflater.inflate(R.layout.kingmall_item_pic,parent,false);
        MyHolder holder=new MyHolder(view);
        holder.imageView = (ImageView) view.findViewById(R.id.iv_goods_pic);
        holder.remove = (ImageView) view.findViewById(R.id.iv_remove);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int i) {
        if(list.size()<9 && i==list.size()){
            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.imageView.setImageResource(R.mipmap.pinagjia_tupian);
            holder.remove.setVisibility(View.GONE);
        }else{
            holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            holder.remove.setVisibility(View.VISIBLE);
            holder.remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deletelistener.onImgDeleteClick(v,i);
                    notifyDataSetChanged();
                }
            });
            Glide.with(mContext)
                    .load(list.get(i))
                    .into(holder.imageView);
        }
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onImgClick( v,list == null ? 0 : list.size());
//                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list.size()<MAX_PCITURE){
            return list.size()+1;
        }else{
            return list.size();
        }
    }

    public void setAddListener(itemOnClickListener listener) {
        this.listener = listener;
    }


    public void setDeleteListener(itemDeleteOnClickListener listener) {
        this.deletelistener = listener;
    }


    public interface itemOnClickListener {
        void onImgClick(View v, int num);
    }
    public interface itemDeleteOnClickListener {
        void onImgDeleteClick(View v, int position);
    }
    class MyHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        ImageView remove;

        public MyHolder(View itemView) {
            super(itemView);
        }
    }
}
